<!--
 * @Author: DaHuaZhuXi
 * @Date: 2020-11-12 16:05:26
 * @LastEditTime: 2020-11-18 15:59:52
 * @LastEditors: DaHuaZhuXi
 * @Description: 登录页面
-->
<template>
  <div class="user-login">
    <el-form class="user-login-form" ref="form" :model="form" :rules="rules">
      <h3>用户登录</h3>
      <el-form-item prop="username">
        <el-input placeholder="用户名" v-model.trim="form.username"></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input placeholder="密码" v-model.trim="form.password"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button block type="primary" @click="clickLogin">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script lang="ts">
import { Component, Vue } from 'vue-property-decorator';
import { phoneReg } from '@/code/regular';

const checkUseranme = (rule: unknown, value: string, callback: Function) => {
  if (phoneReg.test(value)) {
    callback();
  } else {
    callback(new Error('手机号码格式不正确'));
  }
};
@Component
export default class ComponentA extends Vue {
  // @initial data
  $refs!: {
    form: Vue & Form;
  };

  form = {
    username: '',
    password: '',
  };

  rules = {
    username: [
      {
        required: true,
        message: '手机号不可为空',
        trigger: 'blur',
      },
      { validator: checkUseranme },
    ],
    password: [
      {
        required: true,
        message: '密码不可为空',
        trigger: 'blur',
      },
    ],
  };

  // @hook
  mounted() {
    //
  }

  // @computed

  // @method
  clickLogin() {
    // 为表单绑定验证功能
    this.$refs.form.validate((valid: object) => {
      if (!valid) return;
      this.$router.push('/');
    });
  }
}
</script>

<style lang="scss" scoped>
.user-login {
  width: 100%;
  height: 100%;
  background: #000;
  text-align: center;
  display: flex;
  justify-content: center;
  align-items: center;
  color: #fff;
}
.user-login-form {
  h3 {
    margin-bottom: 30px;
    font-size: 24px;
    font-weight: normal;
  }
  width: 400px;
  background: #123050;
  padding: 50px;
  border-radius: 10px;

  .el-button {
    display: block;
    width: 100%;
  }
}
</style>
